package com.myProject.control.medic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TClass;
import com.myProject.bean.TMedicinal;
import com.myProject.bean.TShipment;
import com.myProject.serves.Impl.MedicSevImpl;
import com.myProject.serves.Impl.ShipmentSevImpl;
import com.myProject.serves.MedicSev;
import com.myProject.serves.ShipmentSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "MedicOutServlet", urlPatterns = "/medicOut")
public class MedicOutServlet extends HttpServlet {
    ShipmentSev ss = new ShipmentSevImpl();
    MedicSev ms = new MedicSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:25
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:25
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            addMedicOut(request, response);
        } else if ("addMany".equalsIgnoreCase(med)) {
            addMany(request, response);
        }

    }

    private void addMany(HttpServletRequest request, HttpServletResponse response) {
        Date d = new Date();
        int pkId = Integer.parseInt(request.getParameter("pkId"));
        TMedicinal tm = new TMedicinal();
        tm.setPkId(pkId);
        List<TMedicinal> list = ms.getOneMedic(tm);
        String medicName = list.get(0).getName();
        int number = Integer.parseInt(request.getParameter("number"));
        int fkClassId = Integer.parseInt(request.getParameter("className"));
        String control = list.get(0).getDisastertype();
        String type = list.get(0).getType();
        TClass tc = new TClass();
        TShipment ts = new TShipment();
        tc.setPkId(fkClassId);
        ts.settClass(tc);
        ts.setTime(d);
        ts.setName(medicName);
        ts.setControl(control);
        ts.setType(type);
        ts.setNumber(number);
        Cookie[] cookies = request.getCookies();
        String user = "";
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("loginName".equals(cookie.getName())) {
                    user = cookie.getValue();
                }
            }
        }
        ts.setPeople(user);
        ss.addShipment(ts);
    }

    private void addMedicOut(HttpServletRequest request, HttpServletResponse response) {
        Date d = new Date();
        String medicName = request.getParameter("medicName");
        int number = Integer.parseInt(request.getParameter("number"));
        int fkClassId = Integer.parseInt(request.getParameter("className"));
        String control = request.getParameter("control");
        String type = request.getParameter("type");
        TShipment ts = new TShipment();
        TClass tc = new TClass();
        tc.setPkId(fkClassId);
        ts.settClass(tc);
        ts.setTime(d);
        ts.setName(medicName);
        ts.setControl(control);
        ts.setType(type);
        ts.setNumber(number);
        Cookie[] cookies = request.getCookies();
        String user = "";
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("loginName".equals(cookie.getName())) {
                    user = cookie.getValue();
                }
            }
        }
        ts.setPeople(user);
        ss.addShipment(ts);
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);

        TShipment ts = new TShipment();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        if (date1 != null && !"".equalsIgnoreCase(date1)) {
            try {
                ts.setStartTime(sdf.parse(date1));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (date2 != null && !"".equalsIgnoreCase(date2)) {
            try {
                ts.setEndTime(sdf.parse(date2));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String className = request.getParameter("className");
        TClass tc = new TClass();
        if (className != null && !"".equalsIgnoreCase(className)) {
            tc.setClassName(className);
        }

        ts.settClass(tc);
        System.out.println("tc = " + tc);
        PageInfo<TShipment> show = ss.showShipmentPage(pageNo, pageSize, ts);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);

    }
}
